#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    ui->setupUi(this);

    patientCommunicator = new PatientCommunicator("192.168.233.1", 12345, this);

    // 设置界面布局
    QWidget *centralWidget = new QWidget(this);
    QVBoxLayout *layout = new QVBoxLayout(centralWidget);

    // 请求输入框
    requestInput = new QLineEdit(centralWidget);
    requestInput->setPlaceholderText("输入请求内容...");
    layout->addWidget(requestInput);

    // 响应显示区域
    responseDisplay = new QTextEdit(centralWidget);
    responseDisplay->setReadOnly(true);
    layout->addWidget(responseDisplay);

    // 患者请求按钮
    QPushButton *patientButton = new QPushButton("发送患者请求", centralWidget);
    layout->addWidget(patientButton);
    connect(patientButton, &QPushButton::clicked, this, &MainWindow::onPatientRequest);

    setCentralWidget(centralWidget);

    // 连接患者端和医生端的响应信号到显示槽
    connect(patientCommunicator, &PatientCommunicator::responseReceived, this, &MainWindow::displayPatientResponse);
}

void MainWindow::onPatientRequest()
{
    QString request = "PATIENT_REQUEST:" + requestInput->text();  // 发送的请求内容
    patientCommunicator->sendRequest(request);  // 发送患者请求
}

void MainWindow::displayPatientResponse(const QString &response)
{
    responseDisplay->append(response);  // 显示患者端的响应
}

MainWindow::~MainWindow()
{
    delete ui;
}
